<?php
class Game extends CI_Controller {
	
	/**
	 * Function __construct().
	 * used for Initialize material controller
	 */
	
	function __construct()
	{
		parent::__construct();
		$this->controller = 'game';
		$this->functions->checkAuthAdmin($this->controller.'/');
		
		$this->load->model('Modelgame');
	}

	/**
	 * Function index().
	 * used for material listing
	 */
	
	function index()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$config['base_url'] 			= base_url($this->controller."/index/");

		$config['uri_segment']  		= 3;
		$config['num_links'] 			= 10;
		$config['page_query_string'] 	= false;
		$config['extra_params'] 		= "";
		$config['extra_params'] 		= "";

		$this->pagination->setAdminPaginationStyle($config);
		$start = 0;

		$data['controller'] = $this->controller;

		$param['sortType'] 			= $this->input->request('sortType','ASC');
		$param['sortField'] 		= $this->input->request('sortField','game_name');
		$param['searchField'] 		= $this->input->request('searchField','');
		$param['searchString'] 		= $this->input->request('searchString','');
		$param['searchText'] 		= $this->input->request('searchText','');
		$param['searchDisplay'] 	= $this->input->request('searchDisplay','20');

		$data['recordset'] 		= $this->Modelgame->getList($config,$start,$param);
		
		$data['startRecord'] 	= $start;

		$this->pagination->initialize($config);

		$data['params'] 			= $this->nsession->userdata('ADMIN_GAME');
		$data['reload_link']      		= base_url($this->controller."/index/");
		$data['search_link']       		 = base_url($this->controller."/index/0/1/");
		$data['add_link']         		= base_url($this->controller."/addedit/0/0/");
		$data['edit_link']        		= base_url($this->controller."/addedit/{{ID}}/0");
		$data['delete_link']      		= base_url($this->controller."/delete/{{ID}}/0");
		$data['showall_link']     		= base_url($this->controller."/index/0/1");
		
		$data['active_link']      	= base_url($this->controller."/changestatus/{{ID}}/");
		$data['active_all_link']    = $this->controller."/activeall/";
		$data['inactive_all_link']  = $this->controller."/inactiveall/";
		
		$data['total_rows']			= $config['total_rows'];

		$data['succmsg'] 	= $this->nsession->userdata('succmsg');
		$data['errmsg'] 	= $this->nsession->userdata('errmsg');

		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");

		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'game/index';
		$element_data['menu'] = array();
		$element_data['main'] = $data;

		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);
	}


	/**
	 * Function addedit($id = 0).
	 * used for add edit material
	 * @param $id int material id
	 */

	function addedit($id = 0)
	{
		$this->functions->checkAdmin($this->controller.'/');
		//ifadd or edit
		$startRecord  	= 0;
		$contentId 		= $this->uri->segment(3, 0);
		$page 			= $this->uri->segment(4, 0);

		if($page > 0)
			$startRecord = $page;

		$page = $startRecord;

		$data['controller'] 		= $this->controller;
		$data['action'] 			= "Add";
		$data['params']['page'] 	= $page;
		$data['do_addedit_link']	= base_url($this->controller."/do_addedit/".$contentId."/".$page."/");
		$data['back_link']			= base_url($this->controller."/index/");
		if($contentId > 0)
		{
			$data['adminpage_id'] = $contentId;
			$data['action'] = "Edit";
			//=================prepare DATA ==================
			$rs = $this->Modelgame->getSingle($contentId );
			//pr($rs);
			//$row = $rs->fields;
			
			if(is_array($rs[0]))
			{
				foreach($rs[0] as $key => $val)
				{
					if(!is_numeric($key))
					{
						$data[$key] = $val;
					}
				}
			}

		}else{
			$data['action'] = "Add";
			$data['game_name'] 		= $this->input->request('game_name');
			
			$data['status'] 			= $this->input->request('status','Active');
		}
		//pr($data,0);
		$data['succmsg'] = $this->nsession->userdata('succmsg');
		$data['errmsg'] = $this->nsession->userdata('errmsg');
		$this->nsession->set_userdata('succmsg', "");
		$this->nsession->set_userdata('errmsg', "");

		$elements = array();
		$elements['menu'] = 'menu/index';
		$elements['main'] = 'game/add_edit';

		$element_data['menu'] = array();
		$element_data['main'] = $data;

		$this->layout->setLayout('layout_main_view');
		$this->layout->multiple_view($elements,$element_data);

	}

	/**
	 * Function do_addedit().
	 * used for save material
	 */

	function do_addedit()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$contentId = $this->uri->segment(3, 0); 
		$page = $this->uri->segment(4, 0);

		$game_name		= $this->input->request('game_name');
		$this->form_validation->set_rules('game_name', 'Game Name', 'trim|required|xss_clean');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

		if($this->form_validation->run() == TRUE)
		{
			$isContentExist = $this->functions->existRecords(GAMEMASTER,'game_name',$game_name,'id',$contentId);
			if($isContentExist)
			{
				$this->nsession->set_userdata('errmsg', 'Game already exist');
				redirect(base_url($this->controller."/index/"));
				return true;
			}else{
				
				$data['game_name'] 		= $this->input->request('game_name');
				
				
				$data['status'] = $this->input->request('status');

				if($contentId > 0)   //edit
				{
					
					
					$affected_row = $this->Modelgame->editGame($contentId,$data);

					$this->nsession->set_userdata('succmsg', 'Successfully Game Edited.');
					redirect(base_url($this->controller."/index/"));
					return true;
				}
				else    //add
				{
					$insert_id = $this->Modelgame->addGame($data);

					if($insert_id)
					{
						$this->nsession->set_userdata('succmsg', 'Successfully Game Added.');
						redirect(base_url($this->controller."/index/"));
						return true;
					}
				}
			}

		}
		else
		{
			$this->addedit($contentId,$page);
		}
	}

	/**
	 * Function delete().
	 * used for delete material
	 */

	
	function delete()
	{		
		$this->functions->checkAdmin($this->controller.'/');
		$id = $this->uri->segment(3, 0);
		$del_msg = $this->Modelgame->delete($id);
			$this->nsession->set_userdata('succmsg', 'Successfully Game Deleted.');		
				
		redirect(base_url($this->controller."/index/"));
		return true;
	}
	
	/**
	 * Function deleteall().
	 * used for delete all material
	 */
	
	function deleteall()
	{
		$this->functions->checkAdmin($this->controller.'/');
		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);		
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$del_arr = $this->Modelgame->delete($id);
			}
		if(in_array($del_arr,$id_arr))
			{
			$this->nsession->set_userdata('succmsg', 'Successfully Game Deleted.');
			}
		redirect(base_url($this->controller."/"));
		return true;
	}
	
	/**
	 * Function activeall().
	 * used for active all material
	 */
	
	function activeall()
	{
		$this->functions->checkAdmin($this->controller.'/');

		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->Modelgame->changeStatus($id,'active');
		}
		$this->nsession->set_userdata('succmsg', 'Successfully color Status Changed.');
		redirect(base_url($this->controller."/index/"));
		return true;
	}

	/**
	 * Function inactiveall().
	 * used for inactive all material
	 */
	
	function inactiveall()
	{
		$this->functions->checkAdmin($this->controller.'/');

		$id_str = $this->input->request('check_ids',0);
		$id_arr = explode("^",$id_str);
		foreach($id_arr as $id){
			if($id<>'' && $id<>0)
				$this->Modelgame->changeStatus($id,'Inactive');
		}
		$this->nsession->set_userdata('succmsg', 'Successfully Color Status Changed.');
		redirect(base_url($this->controller."/index/"));
		return true;
	}
    
	/**
	 * Function changestatus().
	 * used for change status of material
	 */
	
	function changestatus()
	{
		$this->functions->checkAdmin($this->controller.'/');

		$page = $this->uri->segment(4, 0);
		$id = $this->uri->segment(3, 0);
		$data['errmsg'] = "";
		$this->Modelgame->changeStatus($id);
		$this->nsession->set_userdata('succmsg','Successfully Color Status Changed.');
		redirect(base_url($this->controller."/index/"));
		return true;
	}
	function file_check($str){
				
		if ($_FILES['image']['name'] == '')
		{
			$this->form_validation->set_message('file_check', 'The %s field is required.');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
		
		
	}


}
?>